package com.gakj.truck.third.api.service;

import com.alibaba.fastjson.JSONObject;
import com.gakj.truck.third.api.mapper.CustomorMapper;
import com.gakj.truck.third.api.mapper.UserDtoMapper;
import com.gakj.truck.third.api.ouman.OumanService;
import com.gakj.truck.third.core.domain.CustomorDto;
import com.gakj.truck.third.core.domain.UserDto;
import com.gakj.truck.third.core.enums.EnumUserType;
import com.gakj.truck.third.core.utils.Clock;
import com.gakj.truck.third.core.utils.Digests;
import com.gakj.truck.third.core.utils.Encodes;
import com.gakj.truck.third.core.utils.RequestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by gc on 2016/5/10.
 */
@Service
public class UserDtoService {

    public static final String HASH_ALGORITHM = "SHA-1";
    public static final int HASH_INTERATIONS = 1024;
    private static final int SALT_SIZE = 8;

    @Autowired
    UserDtoMapper userDtoMapper;

    @Autowired
    CustomorMapper customorMapper;

    @Autowired
    OumanService oumanService;

    private Clock clock = Clock.DEFAULT;


    public JSONObject checkUser(String loginName, String password, String mobile, String type) {

        if(StringUtils.isEmpty(password)){
            return RequestUtils.failure("密码不能为空!");
        }

        CustomorDto user = getUser(loginName, mobile, type);
        if(user==null){
            //TODO 处理入库
            return RequestUtils.failure("无此用户");
        }

        String salt = user.getSalt();
        String entryptPassword = getEntryptPassword(password, salt);
        if(!entryptPassword.equals(user.getPassword())){
            return RequestUtils.failure("密码错误");
        }

        return RequestUtils.success(user);
    }

    public CustomorDto getUser(String loginName,String mobile,String type){

//        UserDto user = userDtoMapper.getUserByLoginNameOrMobile(loginName,mobile,type);
        CustomorDto customorDto = customorMapper.getCustomorByLoginNameOrMobile(loginName, type, mobile);
        return customorDto;

    }


    /**
     * 判断密码是否一致，根据输入的密码和数据库的盐进行加密，比对数据库的密码
     *
     * @param pwd
     * @return
     */
    public String getEntryptPassword(String pwd, String salt)
    {
        byte[] newSalt = Encodes.decodeHex(salt);
        byte[] hashPassword = Digests.sha1(pwd.getBytes(), newSalt, HASH_INTERATIONS);
        return Encodes.encodeHex(hashPassword);
    }

    /**
     * 设定安全的密码，生成随机的salt并经过1024次 sha-1 hash
     */
    public void entryptPassword(UserDto user) {
        byte[] salt = Digests.generateSalt(SALT_SIZE);
        user.setSalt(Encodes.encodeHex(salt));
        byte[] hashPassword = Digests.sha1(user.getPassword().getBytes(), salt, HASH_INTERATIONS);
        user.setPassword(Encodes.encodeHex(hashPassword));
    }

    public JSONObject registerUser(String loginName, String mobile, String password, String type) {
        JSONObject result = new JSONObject();
        UserDto userDto = new UserDto();

        userDto.setDeltag("0");
        userDto.setIsanon("0");
        userDto.setLastlogindate(new Date());
        userDto.setLoginName(loginName);
        userDto.setType(type);
        userDto.setMobile(mobile);
        userDto.setRegisterDate(new Date());
        userDto.setName(mobile);
        userDto.setPassword(password);
        entryptPassword(userDto);
        try {
            userDtoMapper.save(userDto);
            result.put("resMsg","注册成功");
            result.put("resCode","1");
        }catch (Exception ex){
            result.put("resMsg","注册失败");
            result.put("resCode","0");
            ex.printStackTrace();
        }
        return result;
    }

    public boolean checkMobile(String mobile) {
        List<UserDto> list = userDtoMapper.findUserByMobile(mobile);
        return list != null && list.size() > 0;
    }

    /**
     * 注册普通用户，不带密码的
     *
     * @param user
     */
    public void registerUserPwd(UserDto user)
    {
        entryptPassword(user);
        user.setType(EnumUserType.CUSTOMOR.getCode()); // 标记是普通的用户类
        user.setRegisterDate(clock.getCurrentDate());
        userDtoMapper.save(user);
    }
}
